var map;
var directionsDisplay;

function createMap(lat, lng, domID, getTraffic, isDraggable) {
	var rendererOptions = {
	  draggable: isDraggable
	};
	
	directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
	var warehouse = new google.maps.LatLng(lat, lng);
	var myOptions = {
		zoom :12,
		center :warehouse,
		mapTypeId :google.maps.MapTypeId.ROADMAP
	};

	map = new google.maps.Map(document.getElementById(domID),
			myOptions);
	
	directionsDisplay.setMap(map);
	
	if(getTraffic=="google"){
		var trafficLayer = new google.maps.TrafficLayer();
		trafficLayer.setMap(map);
	}else if(getTraffic=="traffy"){
		var Overlay  = new missouristate.web.TileOverlay(
            function(x, y, z) { 
            	return   "http://athena.traffy.in.th/apis/apitraffy.php?format=png&api=getTile&key=AibgZsBJdV&appid=00001368&x=" + x + "&y=" + y + "&z=" + z ; 
            	//return   "http://athena.traffy.in.th/apis/apitraffy.php?format=png&api=getTile&key=EjEGHJ3gTX&appid=00001478&x=" + x + "&y=" + y + "&z=" + z ;
            },
            {
                'map': map, // optional. google.maps.Map reference.
                'visible': true, //optional. boolean. controls initial display of the layer.
                'minZoom': 1, // optional. minimum zoom level at which the tile layer will display.
                'maxZoom': 19, //optional. maximum zoom level at which the tile layer will display.
                'mapTypes': [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.HYBRID], //optional. If present, tiles will only be drawn when the map type matches
                'percentOpacity': 80 //optional. Initial opacity percentage of the overlay.
            }
        );
	}
}
var currentLocation;
var currentLat;
var currentLng;
function getCurrentLocation() {
	if(currentLocation!=null)
		currentLocation.setMap(null);
	// Try W3C Geolocation (Preferred)
	  if(navigator.geolocation) {
		browserSupportFlag = true;
		navigator.geolocation.getCurrentPosition(function(position) {
		currentLat = position.coords.latitude;
		currentLng = position.coords.longitude
    	initialLocation = new google.maps.LatLng(currentLat,currentLng);
    	var image = './resources/images/car.png';
		currentLocation = new google.maps.Marker( {
			position :initialLocation,
			map :map,
			icon :image
		});
		
		currentLocation.setAnimation(google.maps.Animation.BOUNCE);
		google.maps.event.addListener(currentLocation, 'click', toggleBounce);
			function toggleBounce() {
			  if (currentLocation.getAnimation() != null) {
				  currentLocation.setAnimation(null);
			  } else {
				  currentLocation.setAnimation(google.maps.Animation.BOUNCE);
			  }
			}
	    }, function() {
	      handleNoGeolocation(browserSupportFlag);
	    });
	  // Try Google Gears Geolocation
	  } else if (google.gears) {
	    browserSupportFlag = true;
	    var geo = google.gears.factory.create('beta.geolocation');
	    geo.getCurrentPosition(function(position) {
    	currentLat = position.coords.latitude;
		currentLng = position.coords.longitude
    	initialLocation = new google.maps.LatLng(currentLat,currentLng);
	      
	    var image = './resources/images/car.png';
			currentLocation = new google.maps.Marker( {
			position :initialLocation,
			map :map,
			icon :image
		});
			
	    currentLocation.setAnimation(google.maps.Animation.BOUNCE);
		google.maps.event.addListener(currentLocation, 'click', toggleBounce);
			function toggleBounce() {
			  if (currentLocation.getAnimation() != null) {
				  currentLocation.setAnimation(null);
			  } else {
				  currentLocation.setAnimation(google.maps.Animation.BOUNCE);
			  }
			}
	    }, function() {
	      handleNoGeoLocation(browserSupportFlag);
	    });
	  // Browser doesn't support Geolocation
	  } else {
	    browserSupportFlag = false;
	    handleNoGeolocation(browserSupportFlag);
	  }
	  
	  function handleNoGeolocation(errorFlag) {
	    if (errorFlag == true) {
	      alert("Geolocation service failed.");
	    } else {
	      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
	    }
	  }
}
